﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyPhongHoc_ChiTiet : QldtSdh.GUI.Common.BaseDialog
    {
        PhongHoc phongHoc;
        CoSoDaoTao coSo;
        int countEtityTobeAdded = 0;
        public event Common.TListChangedEventHandler OnChanged;
        public FormQuanLyPhongHoc_ChiTiet(CoSoDaoTao coSo, PhongHoc phongHoc = null)
        {
            InitializeComponent();
            this.phongHoc = phongHoc;
            this.coSo = coSo;
            LoadCSDT();
            InitInterface();

        }

        void InitInterface()
        {
            if (phongHoc != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.PHONGHOC);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtTen.Text = phongHoc.TenPhongHoc;
                nHocToiDa.Value = phongHoc.SoLuongHocToiDa;
                nThiToiDa.Value = phongHoc.SoLuongThiToiDa;
                cmbCoSoDaoTao.SelectedValue = phongHoc.MaCoSoDaoTaoGUID;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.PHONGHOC);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtTen.Text = "";
                nHocToiDa.Value = 50;
                nThiToiDa.Value = 50;
                if (coSo != null)
                    cmbCoSoDaoTao.SelectedValue = coSo.MaCoSoDaoTaoGUID;
            }
            txtTen.Focus();
        }

        void LoadCSDT()
        {
            cmbCoSoDaoTao.DataSource = BizCoSoDaoTao.GetList();
            cmbCoSoDaoTao.DisplayMember = "TenCoSoDaoTao";
            cmbCoSoDaoTao.ValueMember = "MaCoSoDaoTaoGUID";
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtTen.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.PHONGHOC));
                txtTen.Focus();
                return false;
            }
            if (cmbCoSoDaoTao.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbCoSoDaoTao, string.Format(ERROR_CONSTANT_STRING.LOI_DANHSACHRONG, ENTITY_NAME.COSODAOTAO));
                btnBoQua.Focus();
                return false;
            }
            //if (txtHocToiDa.Text.IsNullOrEmpty())
            //{
            //    errorNotify.SetError(txtHocToiDa, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY));
            //    txtHocToiDa.Focus();
            //    return false;
            //}
            //if (txtThiToiDa.Text.IsNullOrEmpty())
            //{
            //    errorNotify.SetError(txtThiToiDa, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY));
            //    txtThiToiDa.Focus();
            //    return false;
            //}
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (phongHoc != null)
                {
                    #region Cập nhật thông tin
                    phongHoc.TenPhongHoc = txtTen.Text.Trim();
                    phongHoc.SoLuongThiToiDa = (int)nThiToiDa.Value;
                    phongHoc.SoLuongHocToiDa = (int)nHocToiDa.Value;
                    phongHoc.MaCoSoDaoTaoGUID = cmbCoSoDaoTao.SelectedValue.ToString();
                    var rs = BizPhongHoc.Update(phongHoc);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.PHONGHOC));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.PHONGHOC));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới
                    phongHoc = new PhongHoc();
                    var csdt = cmbCoSoDaoTao.SelectedItem as CoSoDaoTao;
                    phongHoc.MaPhongHoc = csdt.MaCoSoDaoTao + txtTen.Text.Trim();
                    phongHoc.TenPhongHoc = txtTen.Text.Trim();
                    phongHoc.SoLuongThiToiDa = (int)nThiToiDa.Value;
                    phongHoc.SoLuongHocToiDa = (int)nHocToiDa.Value;
                    phongHoc.MaCoSoDaoTaoGUID = cmbCoSoDaoTao.SelectedValue.ToString();
                    var rs = BizPhongHoc.Add(phongHoc);
                    phongHoc = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countEtityTobeAdded++;
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.PHONGHOC));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.PHONGHOC));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyPhongHoc_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countEtityTobeAdded > 0)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
